﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventHandlerExtension.EventsExtension;

namespace EventHandlerExtension
{
    class Program
    {
        static void WriteConsole(string message, ConsoleColor color)
        {
            ConsoleColor previousColor = Console.ForegroundColor;
            Console.ForegroundColor = color;
            Console.WriteLine(message);
            Console.ForegroundColor = previousColor;
        }

        static event EventHandler<ProgramEventArgs> OnFire;

        static void Main(string[] args)
        {
            // bind 3 handlers (1 bad handler)
            OnFire += new EventHandler<ProgramEventArgs>(Test_OnFire);
            OnFire += new EventHandler<ProgramEventArgs>(Test_OnFireBadHandler);
            OnFire += new EventHandler<ProgramEventArgs>(Test_OnFire);

            #region test1
            Console.WriteLine("-TEST-1---------------------------------------------------------");

            try
            {
                WriteConsole("Fire normally:", ConsoleColor.Green);
                if (OnFire != null)
                    OnFire(null, new ProgramEventArgs("This is my message of test1"));
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            WriteConsole("The execution of handlers stops after the exception.", ConsoleColor.DarkGreen);
            #endregion test1




            #region test2
            Console.WriteLine();
            Console.WriteLine("-TEST-2---------------------------------------------------------");
            try
            {
                WriteConsole("Fire safely:", ConsoleColor.Green);
                OnFire.RaiseSafe(null, new ProgramEventArgs("This is my message of test2"));
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            WriteConsole("The execution of handlers doesn't stops after the exception and the exception is thrown afterwards.", ConsoleColor.DarkGreen);
            #endregion test2





            #region test3
            Console.WriteLine();
            Console.WriteLine("-TEST-3---------------------------------------------------------");

            WriteConsole("Fire safely with internal handling:", ConsoleColor.Green);
            OnFire.RaiseSafe(null, new ProgramEventArgs("This is my message of test3"), EventExceptionHandler);
            WriteConsole("The execution of handlers doesn't stops after the exception and each exception is handled by the internal handler.", ConsoleColor.DarkGreen);
            #endregion test3

            Console.WriteLine();
            Console.WriteLine("Press any key");
            Console.ReadKey();

        }

        static void Test_OnFire(object sender, ProgramEventArgs e)
        {
            Console.WriteLine("- Test_OnFire '" + e.Message + "'");
        }

        static void Test_OnFireBadHandler(object sender, ProgramEventArgs e)
        {
            Console.WriteLine("- Test_OnFireBadHandler '" + e.Message + "'");

            // this eventhandler throws an exception!
            throw new Exception("! Test_OnFireBadHandler made an exception !");
        }

        private static void EventExceptionHandler(object sender, EventExceptionArgs<ProgramEventArgs> e)
        {
            Console.WriteLine("! EventExceptionHandler: " + e.Exception.Message);
        }


    }
}
